package com.jyy.prefabricated.service;

import com.jyy.prefabricated.entity.SysPermission;

import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.SysPermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class SysPermissionService {

    @Autowired
    private SysPermissionMapper permissionMapper;

    /**
     * 新增
     * @param permission
     * @return
     */
    public int insert(SysPermission permission){
        permission.setStatus(BaseStatusEnum.VALID.getValue());
        permissionMapper.insert(permission);
        return permission.getId();
    }

    /**
     * 更新
     * @param permission
     * @return
     */
    public int update(SysPermission permission){
        if(permission.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        return permissionMapper.updateByPrimaryKeySelective(permission);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return permissionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public SysPermission selectById(Integer id){
        return permissionMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据参数查询
     * @param pid
     * @param keyword
     * @return
     */
    public List<SysPermission> selectByParams(Integer pid, String keyword){
        return permissionMapper.selectByParams(pid,keyword);
    }

    /**
     * 查询所有权限
     * @return
     */
    public List<SysPermission> selectAll(){
        return permissionMapper.selectAll();
    }

    /**
     * 根据角色查询权限列表
     * @param roleId 角色id
     * @return
     */
    public List<SysPermission> selectRolePermissionByRoleId(Integer roleId){
        return permissionMapper.selectByRoleId(roleId);
    }

}
